package com.supermarket.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

/**
 *跨域配置
 *
 */
@Configuration
public class CorsConfig{

    @Bean
    public CorsFilter corsFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.setAllowedOriginPatterns(Arrays.asList("*")); // 替换addAllowedOrigin("*")
        corsConfiguration.setAllowCredentials(true); // 新增允许凭证
        corsConfiguration.setMaxAge(3600L); // 新增预检缓存时间
        corsConfiguration.addAllowedHeader("*");//2设置访问源请求头
        corsConfiguration.addAllowedMethod("*");//3设置访问源请求方法
        source.registerCorsConfiguration("/**",corsConfiguration);//4对接口配置跨域设置
        return new CorsFilter(source);
    }
}